/*
* created by Administrator on 2018/11/30
* 好的代码像粥一样,是得用时间和精力熬的。
*/
//generator 函数
// yield
let generator = function * (fn) {
    let res1 = yield delay(4000,4);//stop
    fn(res1);
    let res2 = yield delay(4000,4);//stop
    fn(res2);
    let res3 = yield delay(4000,4);//stop
    fn(res3);
    let res4 = yield delay(4000,4);//stop
    fn(res4);
};
// let it = generator();
let it = generator(function (data) {
    console.log(data);
});

function delay(time,value) {
    // setTimeout(function () {
    //     it.next(value);
    // },time)
    return new Promise(function (resolve,reject) {
        setTimeout(function () {
            resolve(value);
        },time)
    })
}
it.next();

// async 异步函数  await【等待】 ==>>promise
//async 函数会返回promise
// async 函数不会阻塞主线程
let d = async function () {
    let a = await delay(1000,5).then((data)=>console.log(data)).catch();
    let b = await delay(2000,6).then((data)=>console.log(data)).catch();
    let c = await delay(3000,7).then((data)=>console.log(data)).catch();
    console.log(123);
    let _d = await delay(4000,8).then((data)=>console.log(data)).catch();
    await a;
    await b;
    await c;
    await _d;
};
d().then(function (data) {
   console.log(data) ;
});


